home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 April / PCgo 2008-04 (DVD).iso / interface / contents / demoversionen_3846 / 13664 / files / Data1.cab / csharptcimagetool.cs < prev    next >
Encoding:
Text File  |  2006-05-31  |  6.5 KB  |  227 lines

  1. using System;
  2. using System.Windows.Forms;
  3. using IMSIGX;
  4. using TCIMAGELib;
  5.  
  6. namespace CSharpTCImage
  7. {
  8.     /// <summary>
  9.     /// Summary description for Class1.
  10.     /// </summary>
  11.     public class CSharpTCImageTool
  12.     {
  13.         const short NUM_TOOLS = 1;
  14.         //Toggle this to test loading buttons from .Bmp/.Res
  15.         const bool boolLoadFromBmp = false;
  16.         public CSharpTCImageTool()
  17.         {
  18.             //
  19.             // TODO: Add constructor logic here
  20.             //
  21.         }
  22.         public string Description
  23.         {
  24.             get
  25.             {
  26.                 return "TurboCAD CSharp Tool";
  27.             }
  28.         }
  29.  
  30.         public bool Run(IMSIGX.Tool  theTool)
  31.         {
  32.             IMSIGX.IApplication gxApp;
  33.             IMSIGX.IDrawing gxDr;
  34.             gxApp = theTool.Application ;
  35.             gxDr = gxApp.ActiveDrawing ;
  36.         
  37.             //MessageBox.Show ("Add your code here");
  38.             InsertPicture(gxApp, gxDr);
  39.             return true;
  40.             
  41.         }
  42.  
  43.         public int GetToolInfo(out object CommandNames, out object MenuCaptions, out object StatusPrompts, out object ToolTips, out object Enabled, out object WantsUpdates)
  44.         {
  45.             try
  46.             {
  47.                 string[] SCommandNames = new string[NUM_TOOLS];
  48.                 string[,] SMenuCaptions = new string[NUM_TOOLS,2];
  49.                 string[] SStatusPrompts = new string[NUM_TOOLS];
  50.                 string[] SToolTips = new string[NUM_TOOLS];
  51.                 bool[] BEnabled = new bool[NUM_TOOLS];
  52.                 bool[] BWantsUpdates = new bool[NUM_TOOLS];
  53.                 int[] iIndex = {0,0};
  54.                 SCommandNames[0] = "Bonus|C# TCImage Tool";
  55.                 SMenuCaptions.SetValue ("C# TCImage Tool",iIndex);
  56.                 iIndex[1] = 1;
  57.                 Array arTest = Array.CreateInstance (typeof(string),1,2);
  58.  
  59.                 arTest.SetValue ("test 1", 0, 0);
  60.                 arTest.SetValue ("test 2", 0, 1);
  61.  
  62.                 SMenuCaptions.SetValue ("Bonus",0,1);
  63.                 //SMenuCaptions[0, 1] = "Bonus";
  64.                 SStatusPrompts[0] = "C# Tool prompt";
  65.                 SToolTips[0] = "C# TCImage Tool tooltip";
  66.  
  67.                 BEnabled[0] = true;
  68.                 BWantsUpdates[0] = false;
  69.             
  70.                 CommandNames = SCommandNames;
  71.                 MenuCaptions = SMenuCaptions;
  72.                 StatusPrompts  = SStatusPrompts;
  73.                 ToolTips = SToolTips;
  74.                 Enabled = BEnabled;
  75.                 WantsUpdates = BWantsUpdates;
  76.  
  77.                 return NUM_TOOLS;
  78.             }
  79.             catch(Exception e)
  80.             {
  81.                 Console.WriteLine("An error occurred: '{0}'", e);
  82.                 throw;
  83.             }
  84.         }
  85.  
  86.         /// <summary>
  87.         /// Public Function GetPicture(ByVal LargeImage As Boolean, ByVal MonoImage As Boolean) As Object
  88.         /// </summary>
  89.         public object GetPicture(bool LargeImage, bool MonoImage)
  90.         {
  91.             System.Drawing.Bitmap TheImage;
  92.             TheImage = null;
  93.             if (GetButtonPicture(LargeImage, MonoImage, ref TheImage ) == true)
  94.             {
  95.                 ImageConverter imgConverter = new ImageConverter() ;
  96.                 return imgConverter.ImageToIpicture(TheImage);
  97.             }
  98.             return null;
  99.         }
  100.  
  101.         public bool CopyBitmap(bool LargeImage, bool MonoImage)
  102.         {
  103.             return true;
  104.         }
  105.  
  106.         public bool Initialize(object Tool)
  107.         {
  108.             return true;
  109.         }
  110.  
  111.         public bool UpdateToolStatus(object Tool, bool Enabled, bool Checked)
  112.         {
  113.             return true;
  114.         }
  115.         private bool GetButtonPicture(bool LargeImage, bool MonoImage, ref System.Drawing.Bitmap  theImage )
  116.         {
  117.             string s, s1;
  118.             bool bRet = false;
  119.             //define correct path to the icons here' 
  120.             //        ' copy of these bmp files are located in TCVBNETTool directory
  121.             //      'There are two ways to load images:  from .Bmp file(s) or from .RES resource.
  122.             //        'In this demo, we control the loading by a private variable.
  123.             //        Dim img As System.Drawing.Image
  124.             //        Dim thisApp As System.Reflection.Assembly
  125.             //        Dim file As System.IO.Stream
  126.             System.Reflection.Assembly thisApp;
  127.             System.IO.Stream file;
  128.             System.Drawing.Image img;
  129.             if (boolLoadFromBmp == true)
  130.             {
  131.                 if (LargeImage)
  132.                 {
  133.                     img = System.Drawing.Image.FromFile(s1); //'"LargeIcon.bmp")
  134.                     bRet = true;
  135.                 }
  136.                 else
  137.                 {
  138.                     img = System.Drawing.Image.FromFile(s); //'"SmallIcon.bmp")
  139.                     bRet = true;
  140.                 }
  141.             }
  142.             else
  143.             {
  144.                                                  
  145.                 if (LargeImage)
  146.                 {
  147.                     thisApp = System.Reflection.Assembly.GetExecutingAssembly();
  148.                     //file = thisApp.GetManifestResourceStream("CSharpTCImage.101.bmp");
  149.                     file = thisApp.GetManifestResourceStream("CSharpTCImage.101.bmp");
  150.                     img = System.Drawing.Image.FromStream(file);
  151.                     bRet = true;
  152.                 }
  153.                 else
  154.                 {
  155.                     thisApp = System.Reflection.Assembly.GetExecutingAssembly();
  156.                     file = thisApp.GetManifestResourceStream("CSharpTCImage.102.bmp");
  157.                     img = System.Drawing.Image.FromStream(file);
  158.                     bRet = true;
  159.                 }
  160.  
  161.             }
  162.             theImage = (System.Drawing.Bitmap)img;
  163.             return bRet;
  164.  
  165.         }
  166.         public bool InsertPicture(IMSIGX.IApplication  gxApp, IMSIGX.IDrawing gxDr)
  167.         {
  168.             ITCImageManager pImageManager;
  169.             string sname = "Test name"; // nameof the item in imagemanager picture's list
  170.             double dx = 100;    // size dx of the picture in drawing
  171.             double dy = 100;    // size dy of the picture in drawing
  172.             bool bReference = true; // the picture added to the imagemanager as reference
  173.  
  174.             IMSIGX.IMatrix pMatrix = null;
  175.             IMSIGX.View pView;
  176.             IGraphic gxGr;
  177.             IMSIGX.Properties pProps;
  178.             IMSIGX.Property pProp;
  179.             string sFileName = "";
  180.             string     sExt = "Pictures (*.bmp;*.gif;*.jpg;*png;*.tif;*.pcx;*.ico;*.tga;*.wmf)|*.bmp;*.gif;*.jpg;*png;*.tif;*.pcx;*.ico;*.tga;*.wmf||";
  181.             
  182.             System.Windows.Forms.OpenFileDialog fileDlg;
  183.             //fileDlg.AddExtension ( sExt);
  184.             fileDlg = new OpenFileDialog ();
  185.             fileDlg.Filter = sExt;
  186.             if (fileDlg.ShowDialog () != DialogResult.OK)
  187.             {
  188.                 return false;
  189.             }
  190.             sFileName = fileDlg.FileName;
  191.             object varPropName = "%#$AUX@_ShowImage";
  192.             object varVal = true; // Show picture
  193.  
  194.             //pImageManager = new TCIMAGELib.TCImageManagerClass();
  195.             pImageManager = (ITCImageManager)gxApp.CreateObject("TCImage.TCImageManager");
  196.             pMatrix = gxDr.UCS ;
  197.             int lImageStyle = pImageManager.AddImageStyle(gxDr, sname, sFileName, bReference);
  198.             gxGr = (IGraphic)pImageManager.CreateImageObject(gxDr, lImageStyle, pMatrix, dx, dy);
  199.             pProps = gxGr.Properties ;
  200.             pProp = pProps.get_Item(ref varPropName);
  201.             pProp.set_Value(1, ref varVal);
  202.             pView = gxDr.ActiveView ;
  203.             pView.ZoomToExtents ();
  204.             return false;
  205.         }
  206.     }
  207.  
  208.     public class ImageConverter : System.Windows.Forms.AxHost 
  209.     { 
  210.         public ImageConverter():base("59EE46BA-677D-4d20-BF10-8D8067CB8B33") 
  211.         { 
  212.         } 
  213.  
  214.         public  stdole.IPictureDisp ImageToIpicture(System.Drawing.Image image) 
  215.         { 
  216.             return (stdole.IPictureDisp)ImageConverter.GetIPictureDispFromPicture(image); 
  217.         } 
  218.  
  219.         public System.Drawing.Image IPictureToImage(stdole.StdPicture picture) 
  220.         { 
  221.             return ImageConverter.GetPictureFromIPicture(picture); 
  222.         } 
  223.     } 
  224.  
  225.     }
  226.  
  227.